package ac.svn.accesscontrol.store;

import java.util.ArrayList;
import java.util.List;

import ac.svn.accesscontrol.utils.Utility;

public class SVNTag implements ISVNItem,Comparable<SVNTag>
{
    private String tagId;
    private String tagText;
    private String tagHeader;
    private int lineNumber;
    private List<String> lines;

    public String getTagText() {
	return tagText;
    }

    public void setTagText(String tagText) {
	this.tagText = tagText;
    }

    public int getLineNumber() {
	return lineNumber;
    }

    public void setLineNumber(int lineNumber) {
	this.lineNumber = lineNumber;
    }

    public void addLine(String line)
    {
	this.lines.add(line);
    }

    public void removeLine(String line)
    {
	this.lines.remove(line);
    }

    public String getTagId() 
    {
	return tagId;
    }

    public SVNTag()
    {
	tagId = Utility.getGUID();
	tagText = "";
	lineNumber = 0;
	this.lines = new ArrayList<String>(0);
    }

    public SVNTag(String tagText, int lineNumber) 
    {
	super();
	this.tagId = Utility.getGUID();
	this.tagText = tagText;
	this.lineNumber = lineNumber;
	this.lines = new  ArrayList<String>();
    }

    public List<String> getLines() 
    {
	return lines;
    }

    public void setLines(List<String> lines) 
    {
	this.lines = lines;
    }

    public String getTagHeader() 
    {
	return tagHeader;
    }

    public void setTagHeader(String tagHeader) 
    {
	this.tagHeader = tagHeader;
    }

    @Override
    public String toString() 
    {
	String toString = "SVNTag: " + tagText
	+ "\nLineNumber:" + lineNumber
	+ "\nTagHeader:\n" +( (null ==tagHeader)?"":tagHeader)
	+ "\n-----------------------|Operations|-------------------------\n";
	for(String line :lines)
	{
	    toString += line + "\n";
	}
	return toString;
    }

    @Override
    public String getName() 
    {
	return String.format("[%5d] %s",lineNumber,getFixedWidthTagText(50));
    }

    public String getFixedWidthTagText(final int width)
    {
	//Length = 10,
	//Width  = 5,
	
	
	
	String fixedTagText = this.tagText;	
	if((tagText.length()+3)> width)
	{
	    int startIndex = (tagText.length() - (width+4));
	    String compressedString = "";
	    try
	    {
		compressedString = tagText.substring(startIndex,tagText.length());
		fixedTagText = String.format("[..%s",compressedString);
	    }
	    catch(Exception e)
	    {
		fixedTagText = tagText; 
	    }	    
	}		
	return fixedTagText;
    }

    @Override
    public String getTooltipText() 
    {
	return getHTMLString();
    }

    public String getHTMLString()
    {
	String htmlString = String.format("<HTML>Header:%s<br>" +
		"Tag: %s<br>" +
		"Line:%d<br>" +
		"<b>Details</b><br>",
		(this.tagHeader == null)?"No Header":this.tagHeader,
			this.tagText,		
			this.lineNumber);
	for(String line :lines)
	{
	    htmlString += line + "<br>";
	}
	htmlString+="</HTML>";
	return htmlString;
    }

    @Override
    public String getId() 
    {
	return tagId;
    }

    @Override
    public int compareTo(SVNTag o) 
    {
	return (getLineNumber() - o.getLineNumber());
    }

    @Override
    public String getMetaData() {
	return String.format("%s|%s|%d",tagHeader,tagText,lineNumber);
    }
}
